/*
 * @Author: guoyangf
 * @Date: 2020-07-06 15:09:29
 * @LastEditTime: 2020-09-21 16:18:34
 * @Description: 详情页头部按钮
 */
import { toast, promptBox, print } from "nc-lightapp-front";

export default function jobButtonEvent() {

    const { userId } = window.parent.GETBUSINESSINFO();

    return {
        //操作记录
        'Operation': async () => {
            this.historyCtrlInst.query(
                this.currentGridData
            );
            return false;
        },
        'ApproveHistory': async () => {
            this.historyCtrlInst.query(
                this.currentGridData,
                '/nccloud/ssctp/sscbd/SSCTaskHandleQueryOperation4QMAction.do'
            );
            return false;
        },
        //审核
        Approve: async () => {
            let grid = this.currentGridData;
            let param = {
                ...grid,
                userjson: JSON.stringify({
                    reason: this.state.handleReason,
                    skipcodes: '',
                    ntbCheck: true,
                    actiontype: "sscapprove",
                    ntbCheck: this.ntbCheck || false,
                    jkCheck: this.jkCheck || '',
                    ...this.state.sideBarQueryUrl && this.auditPointBarInst.getAuditData(true)
                })
            }
            let factor = this.currentRowData.module.value;

            let result = await this.requestApi.taskApproveAction(param, factor);
            if (result.success) {
                if (result.data && result.data['bugetAlarm']) { //预算预警
                    promptBox({
                        color: 'warning',       // 提示类别默认"success"， "success"/"info"/"warning"/"danger",非必输
                        title: this.state.multiLang['job-approve-tsxx'],//"提示信息"
                        content: this.state.multiLang['job-approve-wxts'] + data.data['bugetAlarm'] + this.state.multiLang['job-approve-sfjx'],
                        noFooter: false,  // 是否显示底部按钮(确定、取消),默认显示(false),非必输
                        noCancelBtn: false,    // 是否显示取消按钮,，默认显示(false),非必输
                        beSureBtnName: this.state.multiLang['job-common-qd'],
                        cancelBtnName: this.state.multiLang['job-common-qx'],
                        beSureBtnClick: () => {
                            this.ntbCheck = true;
                            this.skipcodes = result.data['skipcodes'];
                            //再调用一次审批函数
                            this.jobButtonEvent['Approve']()
                        }
                    })
                } else {
                    toast({ title: this.state.multiLang['job-approve-shcg'] });

                    this.currentRowData.taskstatus.value = this.presetVar.sscApproveBillStatusEnum.sscapprove;
                    this.currentRowData.ts.value = result.data && result.data.ts;

                    this.setButtonStatus();
                }
                this.reloadSubPage();
            }
        },
        //取消审核
        CancelApprove: async () => {
            let grid = this.currentGridData;
            let param = {
                ...grid,
                userjson: JSON.stringify({
                    actiontype: "un-sscapprove"
                })
            }
            let factor = this.currentRowData.module.value;
            let result = await this.requestApi.taskUnApproveAction(param, factor);
            if (result.success) {
                toast({ title: this.state.multiLang['job-approve-qxshcg'] });
                this.currentRowData.taskstatus.value = this.presetVar.sscApproveBillStatusEnum.taken;
                this.currentRowData.ts.value = result.data && result.data.ts;

                this.setButtonStatus();
                this.reloadSubPage();
            }
        },
        //驳回
        Reject: async () => {
            this.rejectModalCtrlInst.show(this.currentGridData);
        },
        //申请调整
        'AdjustApply': async () => {
            this.reasonModalCtrlInst.show();
            this.setState({
                reasonModalConfirmBtnName: this.state.multiLang['job-common-sqtz'], /*申请调整*/
                reasonModalTextareaPlaceholder: this.state.multiLang['job-common-sqtzyy'], /**请输入申请调整原因*/
                reasonModalSaveUrl: '/nccloud/ssctp/sscbd/SSCTaskHandleAdjustAction.do',
                reasonModalActionType: 'adjust'
            });
            return false; //禁止afterEvent

        },
        //取消调整
        'CancelAdjust': async () => {
            this.reasonModalCtrlInst.show();
            this.setState({
                reasonModalConfirmBtnName: this.state.multiLang['job-common-qxtz'], /*取消调整*/
                reasonModalTextareaPlaceholder: this.state.multiLang['job-common-qxtzyy'], /*取消调整原因*/
                reasonModalSaveUrl: '/nccloud/ssctp/sscbd/SSCTaskHandleUnAdjustAction.do',
                reasonModalActionType: 'un-adjust'
            })
            return false; //禁止afterEvent
        },
        //挂起
        'HangUp': () => {
            this.reasonModalCtrlInst.show();
            this.setState({
                reasonModalConfirmBtnName: this.state.multiLang['job-common-gq'], /*挂起*/
                reasonModalTextareaPlaceholder: this.state.multiLang['job-common-gqyy'], /*请输入挂起原因*/
                reasonModalSaveUrl: '/nccloud/ssctp/sscbd/SSCTaskHandleHandonAction.do',
                reasonModalActionType: 'handon',
                reasonModalUserJsonData: {
                    ...this.state.sideBarQueryUrl && this.state.showDetailPage && this.auditPointBarInst.getAuditData(true)
                }
            })
            return false; //禁止afterEvent
        },
        //取消挂起
        'CancelHangUp': () => {
            this.reasonModalCtrlInst.show();
            this.setState({
                reasonModalConfirmBtnName: this.state.multiLang['job-common-qxgq'], /*取消挂起*/
                reasonModalTextareaPlaceholder: this.state.multiLang['job-common-qxgqyy'], /*请输入取消挂起原因*/
                reasonModalSaveUrl: '/nccloud/ssctp/sscbd/SSCTaskHandleUnHandonAction.do',
                reasonModalActionType: 'un-handon'
            })
            return false; //禁止afterEvent
        },


        //任务提取
        'Take': async () => {
            let result = await this.requestApi.taskTakeAction();
            if (result.success) {
                this.queryData();
            }
        },

        'FetchTake': async () => {
            if (!this.state.useDirectionalTakeOrderBtn) {

                this.setState({
                    useDirectionalTakeOrderBtn: true
                }, () => {
                    document.getElementById('ssc-biz-directional-take-btn').click();
                })
            }
            document.getElementById('ssc-biz-directional-take-btn').click();

        },

        /* 稽核专属按钮 */
        //稽核
        'Audit': async () => {
            let currentRowData = this.currentRowData;

            let param = {
                billid: currentRowData.busiid.value || "",
                taskid: currentRowData.def4.value || "",
                ts: currentRowData.ts.value || "",
                processor: userId,
                checkResult: 'PASS' //
            }

            let auditData = this.auditPointBarInst.getAuditData();
            param.points = auditData.points;
            param.problems = auditData.problems;

            if (param.problems.length) {
                param.checkResult = 'PROBLEM'
            }
            let result = await this.requestApi.audit(param);
            if (result.success) {
                this.auditPointBarInst.updateOriginData();
                if (param.problems.length > 0) {
                    toast({ title: this.state.multiLang['job-audit-jhcg'] }); /* 稽核成功 */
                } else {
                    toast({ title: this.state.multiLang['job-audit-wwtttcg'] }); /* 无问题通过成功 */
                }

                this.currentRowData.def2.value = "CHECKED";
                this.currentRowData.taskstatus.value = this.presetVar.sscApproveBillStatusEnum.sscapprove;
                this.currentRowData.ts.value = result.data && result.data.ts;
                this.setButtonStatus();

            }
        },
        //取消稽核
        'CancelAudit': async () => {
            let currentRowData = this.currentRowData;
            let param = {
                billid: currentRowData.busiid.value,
                taskid: currentRowData.def4.value
            }
            let result = await this.requestApi.cancelAudit(param);

            if (result.success) {
                toast({ title: this.state.multiLang['job-audit-qxjhcg'] });

                this.currentRowData.def2.value = "CHECKING";
                this.currentRowData.taskstatus.value = this.presetVar.sscApproveBillStatusEnum.taken;
                this.currentRowData.ts.value = result.data && result.data.ts;
                this.setButtonStatus();

            }

        },
        //暂存
        'TempSave': async () => {

            let currentRowData = this.currentRowData;
            let param = {
                billid: currentRowData.busiid.value || "",
                taskid: currentRowData.def4.value || "",
                ts: currentRowData.ts.value || "",
                processor: userId,
                checkResult: 'TEMP' //
            }

            let auditData = this.auditPointBarInst.getAuditData();
            param.points = auditData.points;
            param.problems = auditData.problems;

            let result = await this.requestApi.saveTemp(param);
            if (result.success) {
                this.auditPointBarInst.updateOriginData();
                toast({ title: this.state.multiLang['job-audit-bccg'] }); /*保存成功*/

                this.currentRowData.def2.value = "CHECKING";
                this.currentRowData.def3.value = 'TEMP';
                this.currentRowData.taskstatus.value = this.presetVar.sscApproveBillStatusEnum.taken;
                this.currentRowData.ts.value = result.data && result.data.ts;
                this.setButtonStatus();
            }
        },
        //整改
        Rectify: async () => {
            let param = {
                billid: this.currentRowData.busiid.value || "",
                taskid: this.currentRowData.def4.value || "",
                ts: this.currentRowData.ts.value || "",
                processor: userId,
                rectifyResult: "RECTIFY",
            }

            let auditData = this.auditPointBarInst.getAuditData();
            param.points = auditData.points;
            param.problems = auditData.problems;
            param.rectify_desc = auditData.rectify_desc; //单据整改描述

            let result = await this.requestApi.rectify(param);

            //this.currentRowData.check_status.value = "unChecked";
            if (result.success) {
                toast({ title: this.state.multiLang['job-audit-zgcg'] }); /*整改成功*/

                this.currentRowData.def5.value = 'RECTIFIED';
                this.currentRowData.ts.value = result.data && result.data.ts;
                this.setButtonStatus();
            }
        },
        //取消整改
        CancelRectify: async () => {
            let param = {
                billid: this.currentRowData.busiid.value,
                taskid: this.currentRowData.def4.value
            }
            let result = await this.requestApi.cancelRectify(param);

            if (result.success) {
                toast({ title: this.state.multiLang['job-audit-zgcg'] }); /*取消整改成功*/

                this.currentRowData.def5.value = 'UNRECTIFY';
                this.currentRowData.ts.value = result.data && result.data.ts;
                this.setButtonStatus();
            }
        },
        //修改
        Edit: async () => {
            this.currentRowData.def3.value = '';
            this.setButtonStatus();
        },
        //取消
        Cancel: async () => {
            let multiLang = this.state.multiLang;
            promptBox({
                color: 'warning',
                title: multiLang['job-common-qx'], /*取消*/
                content: multiLang['job-common-qdqx'], /*确定要取消吗？*/
                beSureBtnName: multiLang['job-common-qd'],/*确定*/
                cancelBtnName: multiLang['job-common-qx'],/*取消*/
                beSureBtnClick: () => {

                    //重置侧边栏状态
                    this.auditPointBarInst.resetData();

                    this.currentRowData.def3.value = 'TEMP';
                    this.setButtonStatus();

                },
                cancelBtnClick: null  // 取消按钮点击调用函数,非必输
            })
        },

        /* 以下为我的打印按钮 */
        //打印单据
        billprint: async () => {
            this.commonPrint('/nccloud/ssctp/print/PrintBillAction.do');
        },
        //打印凭证
        voucherprint: async () => {
            this.commonPrint('/nccloud/ssctp/print/PrintVoucherAction.do');
        },
        //一键打印
        oneclickprint: async (key, record) => {
            this.commonPrint('/nccloud/ssctp/print/PrintBillAndVoucherAction.do', record);
            return false;
        },
        //退回
        rollback: async () => {
            let result = await this.requestApi.printDelete({
                busiid: this.currentRowData.busiid.value
            })
            if (result.success) {
                toast({ title: this.state.multiLang['job-file-thcg'] }); /* 退回成功！ */
            }
        },
        //取消打印
        cancelprint: async () => {
            let result = await this.requestApi.printCancel({
                busiid: this.currentRowData.busiid.value
            })
            if (result.success) {
                toast({ title: this.state.multiLang['job-file-qxdycg'] }); /*取消打印成功*/
            }
        }
    }
}
